package com.lanou.tipsy.fragment.rank;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.alibaba.fastjson.JSON;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.toolbox.JsonObjectRequest;
import com.lanou.tipsy.R;
import com.lanou.tipsy.adapter.rank.RankListAdapter;
import com.lanou.tipsy.base.BaseFragment;
import com.lanou.tipsy.base.Constants;
import com.lanou.tipsy.entity.rank.ContentResult;
import com.lanou.tipsy.entity.rank.RankReuslt;
import com.lanou.tipsy.tools.volley.ResponseErrorListener;
import com.lanou.tipsy.tools.volley.VolleySingleton;

import org.json.JSONObject;

import java.util.ArrayList;


/**
 * Created by  wangshuai on 15/10/20.
 * 排行
 */
public class RankFragment extends BaseFragment {
    public static final String TAG = "RankFragment";
    //网络拉取
    private VolleySingleton volleySingleton;
    private ResponseErrorListener errorListener;
    private JsonObjectRequest request;//请求队列

    private ArrayList<ContentResult> data; //实体类
    private RankListAdapter adapter; //适配器
    private ListView rankLV; //列表

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fr_rank, null);
        return v;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        init(getView());//初始化数据
    }

    private void init(View view) {
        //绑定布局
        rankLV = (ListView) view.findViewById(R.id.rankLV);
        volleySingleton = VolleySingleton.getInstance();
        errorListener = new ResponseErrorListener();
        //添加匿名监听
        rankLV.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                RankInfoFragment fragment = new RankInfoFragment();
                //获取信息
                String songUrl = data.get(position).getPic_s210();
                String name = data.get(position).getName();
                int type = data.get(position).getType();
                //bundle传值
                Bundle mBundle = new Bundle();
                //图片网址传递
                mBundle.putString("songUrl", songUrl);
                //歌榜名称传递
                mBundle.putString("name", name);
                mBundle.putInt("type", type);
                fragment.setArguments(mBundle);
                FragmentManager manager = getActivity().getSupportFragmentManager();
                FragmentTransaction transaction = manager.beginTransaction();
                transaction.addToBackStack(null);
                transaction.replace(R.id.fl_main, fragment);
                transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
                transaction.commit();
            }
        });

        request = new JsonObjectRequest(Request.Method.GET, Constants.RANK_RUL, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {

                // 将请求下来的 response转换为字符串
                String str = response.toString();
                // 解析
                // 最外层实体类接收解析结果
                RankReuslt root = JSON.parseObject(str, RankReuslt.class);
                data = root.getContent();
                adapter = new RankListAdapter(getActivity(), data);

                rankLV.setAdapter(adapter);

            }
        }, errorListener);
        //绑定队列
        volleySingleton.addQueue(request, TAG);
    }

    @Override
    public void onStop() {
        super.onStop();
        volleySingleton.removeRequest(TAG);
    }
}
